<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改账号</title>
    <style>
        .wu-m5>input{
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
<div class="pos-r">
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span>
        系统管理
        <span class="c-gray en">&gt;</span>
        账户管理
        <span class="c-gray en">&gt;</span>
        修改账号
    </nav>
    <div style="margin-right:450px;">
        <div class="page-container">
            <form action="" method="get" class="form form-horizontal" id="edit-form-account">
                <input style="display: none;" id="accountEdit-userNames-temp"/>
                <input style="display: none;" id="accountEdit-bizId" name="bizId"/>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>联系电话：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" value="" placeholder="必填项" id="accountEdit-phones" name="phone">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>账号：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="必填项" name="userNames" id="accountEdit-userNames" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>密码：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="password" class="form-control" placeholder="不修改无需填写" name="password" id="accountEdit-passwords">
                    </div>
                </div>
                <div class="form-group" id="accountEdit-div">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>角色：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="" name="roleId" id="accountEdit-roleIds">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>状态：</label>
                    <div class="formControls col-xs-4 col-sm-4 skin-minimal">
                        <div class="radio-box">
                            <input type="radio" id="accountEdit-state-1" name="bizState" value="0" checked>
                            <label for="accountEdit-state-1">开启</label>
                        </div>
                        <div class="radio-box">
                            <input type="radio" id="accountEdit-state-2" name="bizState" value="1">
                            <label for="accountEdit-state-2">禁用</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>管理者：</label>
                    <div class="formControls col-xs-4 col-sm-4 skin-minimal">
                        <div class="radio-box">
                            <input type="radio" id="accountEdit-type-1" name="bizType" value="3" checked>
                            <label for="accountEdit-type-1">是</label>&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="radio-box">
                            <input type="radio" id="accountEdit-type-2" name="bizType" value="4">
                            <label for="accountEdit-type-2">否</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input onclick="platformSystemAccountEditFun.submitAction();return false;" class="btn btn-primary radius" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    //商户管理，添加服务
    var platformSystemAccountEditFun;

    $(function () {
        //加载角色
        $.ajax({
            type: "GET",
            url: "menu/getBizRoles",
            dataType: "json",
            success: function (data) {
                $("#accountEdit-roleIds").select2({
                    data: data,
                    placeholder: '请选择',
                    allowClear: true
                })
            },
            error: function () {
                alert("error");
            }
        });
    });

    platformSystemAccountEditFun = {
        init: function () {
            let _this = this;
            var bizId = sessionStorage.getItem("account-bizId");
            var bizIds = sessionStorage.getItem("bizId");
            if(bizId == bizIds){
                $("#accountEdit-div").remove();
            }
            _this.modify_array(bizId);
            _this.bootstrapvalidatorForm();

        },
        //修改，查询单条
        modify_array: function (index) {
            $.ajax({
                type: "get",
                url: "biz/getBizTenant",
                async: false,
                data: {bizId: index},
                dataType: "json",
                success: function (result) {
                    if (result.state) {
                        var data = result.data;
                        $("#accountEdit-phones").val(data.phone);
                        $("#accountEdit-userNames").val(data.userName);
                        $("#accountEdit-userNames-temp").val(data.userName);
                        $("#accountEdit-bizId").val(data.bizId);
                        var type = data.bizType;
                        $("#accountEdit-type-" + (type - 2)).prop("checked", true);
                        var state = data.bizState;
                        $("#accountEdit-state-" + (state + 1)).prop("checked", true);
                        var ddds = $("#accountEdit-roleIds").select2();    //获取selectid
                        ddds.val(data.roleId).trigger("change");    //设置 value 为four的 option 为选中状态
                    }
                    else {
                        alert(result.message);
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //表单提交
        submitAction: function () {
            //bootstrap验证
            let bootstrapValidator = $("#edit-form-account").data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid())
                $("#edit-form-account").submit();
            else return;

//            var treeObj = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//            var nodes = treeObj.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
//            var array = new Array();
//            for (var i = 0; i < nodes.length; i++) {
//                if($("#wuEdit"+nodes[i].id).val() == ""){
//                    array.push(0);
//                    continue;
//                }
//                array.push(nodes[i].id);
//                array.push($("#wuEdit"+nodes[i].id).val()*100);
//            }
//            $("#edit-merchantArray").val(array);

            //提交
            $.ajax({
                type: "GET",
                url: "biz/modifyTenant",
                data: $("#edit-form-account").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.state) {
                        layer.msg(data.message, {icon: 1});
                        //关闭窗口
                        removeIframe();
                        platformSystemAccountFun.searchAction();
                    } else {
                        $("#accountEdit-usernameSpan").text(data.message);
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //bootstrap验证
        bootstrapvalidatorForm: function () {
            $('#edit-form-account').bootstrapValidator({
                message: 'This value is not valid',
                excluded : [':disabled'],
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    phone: {
                        validators: {
                            notEmpty: {
                                message: '联系电话不能为空'
                            },
                            regexp: {
                                regexp: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/,
                                message: '联系电话格式不正确'
                            }
                        }
                    },
                    userNames: {
                        validators: {
                            notEmpty: {
                                message: '账号不能为空'
                            },
                            stringLength: {
                                min: 4,
                                max: 50,
                                message: '账号长度必须在4到50位之间'
                            },
                            threshold:4,
                            remote:{
                                url:"biz/checkTenantName",
                                data:{userNameHide:$('#accountEdit-userNames-temp').val()},
                                message: "该账号已存在",
                                delay: 2000,
                                type:"GET"
                            }
                        }
                    },
                    password: {
                        validators: {
                            stringLength: {
                                min: 6,
                                max: 32,
                                message: '密码长度必须在6到32位之间'
                            }
                        }
                    }
                }
            });
        }
    };

    platformSystemAccountEditFun.init();

</script>
</body>
</html>